import { setToken, http, clearToken } from '@/utils';

// 登录的异步action
export const login = (mobile, code) => {
  return async (dispatch) => {
    // 发起异步请求
    console.log('异步action', mobile, code);
    const res = await http.post('/authorizations', { mobile, code });
    console.log('res', res);

    // dispatch action
    dispatch({ type: 'login/token', payload: res.data.data.token });

    // token 本地存储一份
    // localStorage.setItem('geek-pc-token', res.data.data.token);
    setToken(res.data.data.token);
  };
};

// 推出登录action
export const logout = () => {
  return (dispatch) => {
    // 1 清除本地
    clearToken();

    // 2 清除token
    dispatch({ type: 'login/logout' });

    // 3 清除user信息
    dispatch({ type: 'user/clearInfo' });
  };
};
